package fu.capstone.hanoicityguide.presentation.fragment.itinerary;

import java.util.ArrayList;

import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.view.MapView;
import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.overlay.Marker;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.itinerary.ItineraryBO;
import fu.capstone.hanoicityguide.business.bo.itinerary.ItineraryBOImp;
import fu.capstone.hanoicityguide.business.model.Place;
import fu.capstone.hanoicityguide.common.offlinemapultility.OfflineMapUtil;
import fu.capstone.hanoicityguide.common.utility.LocationSetting;

public class ItineraryOfflineMapFragment extends Fragment {

	private MapView mapview;
	private OfflineMapUtil util;
	private LocationSetting locationSetting;
	private ItineraryBO itineraryBO;
	private ArrayList<Place> arrPlace;
	private LatLong place_geo;
	private ArrayList<LatLong> arr_itinerary_geo;

	public ItineraryOfflineMapFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View itineraryOfflineMapFragment = inflater.inflate(
				R.layout.offline_map_fragment, container, false);

		try {
			util = new OfflineMapUtil(container.getContext());
			mapview = (MapView) itineraryOfflineMapFragment
					.findViewById(R.id.mapView);
			locationSetting = new LocationSetting(container.getContext());

			if (!locationSetting.getGPSSetting(container.getContext())) {
				locationSetting.showSettingsAlert();
			} else {
				/* get data */
				Bundle bundle = this.getArguments();
				int itineraryID = bundle.getInt("ItineraryID", 0);
				if (itineraryID != 0) {

					itineraryBO = new ItineraryBOImp(container.getContext());
					arrPlace = itineraryBO.browseItinerary(itineraryID);
					util.setupofflinemap(mapview, container.getContext());

					Layers layers = this.mapview.getLayerManager().getLayers();

					LatLong mylocation = util.getcurrentLocation(
							container.getContext(), mapview);

					Marker mylocationMarker = util.createMyLocationMarker(
							container.getContext(), mylocation,
							R.drawable.mylocationmarker);

					layers.add(mylocationMarker);

					for (int i = 0; i < arrPlace.size(); i++) {
						arrPlace.get(i).getGeo();
						place_geo = util.convertGeoCoordinateToLatLong(arrPlace
								.get(i).getGeo());
						Marker tempMarker = util.createMarker(container
								.getContext(), place_geo, arrPlace.get(i)
								.getName(), arrPlace.get(i).getCategory());
						layers.add(tempMarker);
					}

					// int temp = 0;
					util.initHopper();
					util.calcPath(mylocation.latitude, mylocation.longitude,
							util.convertGeoCoordinateToLatLong(arrPlace.get(0)
									.getGeo()).latitude, util
									.convertGeoCoordinateToLatLong(arrPlace
											.get(0).getGeo()).longitude,
							mapview);
					for (int i = 0; i < arrPlace.size() - 1; i++) {
						util.convertGeoCoordinateToLatLong(arrPlace.get(i)
								.getGeo());
						util.calcPath(util
								.convertGeoCoordinateToLatLong(arrPlace.get(i)
										.getGeo()).latitude, util
								.convertGeoCoordinateToLatLong(arrPlace.get(i)
										.getGeo()).longitude, util
								.convertGeoCoordinateToLatLong(arrPlace.get(
										i + 1).getGeo()).latitude, util
								.convertGeoCoordinateToLatLong(arrPlace.get(
										i + 1).getGeo()).longitude, mapview);
					}

				}
			}
		} catch (Exception e) {
			Log.v("Itinerary offline map", e.toString());
		}
		return itineraryOfflineMapFragment;
	}
}
